/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.resource.service;

import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.service.CrudService;
import com.lczyfz.istep.modules.resource.dao.ResourceCourseCoursesystemDao;
import com.lczyfz.istep.modules.resource.entity.ResourceCourseCoursesystem;
import com.lczyfz.istep.modules.resource.entity.ResourceCoursesystem;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 课程-课程体系中间表Service
 * @author dh
 * @version 2017-02-21
 */
@Service
@Transactional(readOnly = true)
public class ResourceCourseCoursesystemService extends CrudService<ResourceCourseCoursesystemDao, ResourceCourseCoursesystem> {


	public ResourceCourseCoursesystem get(String id) {
		return super.get(id);
	}
	
	public List<ResourceCourseCoursesystem> findList(ResourceCourseCoursesystem resourceCourseCoursesystem) {
		return super.findList(resourceCourseCoursesystem);
	}
	
	public Page<ResourceCourseCoursesystem> findPage(Page<ResourceCourseCoursesystem> page, ResourceCourseCoursesystem resourceCourseCoursesystem) {
		return super.findPage(page, resourceCourseCoursesystem);
	}
	
	@Transactional(readOnly = false)
	public void save(String duallist,ResourceCoursesystem resourceCoursesystem) {
		String[] resourceCourseList = new String[0];
		if (duallist != null && !duallist.equals(""))
			resourceCourseList = duallist.split(",");
		logger.debug("length: [{}]", resourceCourseList.length);

		for (int i = 0; i < resourceCourseList.length; i++) {

			ResourceCourseCoursesystem resourceCourseCoursesystem=new ResourceCourseCoursesystem();
			resourceCourseCoursesystem.setCourseId(resourceCourseList[i]);
			resourceCourseCoursesystem.setCoursesystemId(resourceCoursesystem.getId());
			super.save(resourceCourseCoursesystem);
		}


	}
	@Transactional(readOnly = false)
	public void update(String duallist,ResourceCoursesystem resourceCoursesystem) {
		ResourceCourseCoursesystem temp=new ResourceCourseCoursesystem();
		temp.setCoursesystemId(resourceCoursesystem.getId());
		List<ResourceCourseCoursesystem> courseCoursesystemList=super.findList(temp);
		logger.debug("课程-课程体系中间表数量: [{}]", courseCoursesystemList.size());

		for(int i=0;i<courseCoursesystemList.size();i++)
		{
			super.delete(courseCoursesystemList.get(i));
		}
		String[] courseList = new String[0];
		if (duallist != null && !duallist.equals(""))
			courseList = duallist.split(",");
		logger.debug("length: [{}]", courseList.length);
		for (int i = 0; i < courseList.length; i++) {

			ResourceCourseCoursesystem resourceCourseCoursesystem=new ResourceCourseCoursesystem();
			resourceCourseCoursesystem.setCoursesystemId(resourceCoursesystem.getId());
			resourceCourseCoursesystem.setCourseId(courseList[i]);
			super.save(resourceCourseCoursesystem);
		}
	}
	@Transactional(readOnly = false)
	public void delete(ResourceCourseCoursesystem resourceCourseCoursesystem) {
		super.delete(resourceCourseCoursesystem);
	}
	
}